package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiCheckVerificationCode;
import com.yjt.fresh.api.ApiGetConfirmCode;
import com.yjt.fresh.api.ApiPhoneIsExist;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanCity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.CommonsToolsHelper;
import com.yjt.fresh.utils.PreferenceManager;
import com.yjt.fresh.widget.CustomToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;

/**
 * 注册页面
 * Created by mcc on 2018-07-17.
 */

public class AtyRegister extends BaseSimpleActivity {
    @BindView(R.id.rlBack)
    RelativeLayout mBack;
    @BindView(R.id.edtAppAtyRegisterPhone)
    EditText mEdtPhone;
    @BindView(R.id.btnAppAtyRegisterGetConfirmCode)
    Button mBtnGetConfirmCode;
    @BindView(R.id.edtAppAtyRegisterConfirmCode)
    EditText mEdtConfirmCode;
    @BindView(R.id.cbAppAtyRegisterProtocol)
    CheckBox mCbProtocol;
    @BindView(R.id.tvAppAtyRegisterProtocol)
    TextView mTvProtocol;
    @BindView(R.id.btnAppAtyRegisterNext)
    Button mBtnNext;
    private boolean isGetConfirmCode;
    private boolean isPutConfirmCode;
    private boolean isProtocolChecked;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_register;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initClick();
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mTvProtocol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyUserArgument.class);
                startActivity(intent);
            }
        });

        mBtnGetConfirmCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEdtPhone.getText().toString())) {
                    CustomToast.showLong("请输入手机号码");
                    mEdtPhone.requestFocus();
                    return;
                }

                if (!CommonsToolsHelper.isMobileNum(mEdtPhone.getText().toString())){
                    CustomToast.showLong("请输入合法的手机号码");
                    mEdtPhone.requestFocus();
                    return;
                }

                getConfirmCode();
            }
        });
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext,AtySetPassword.class);
                intent.putExtra(MyConstants.PHONE_NUM,mEdtPhone.getText().toString());
                intent.putExtra(MyConstants.CONFIRM_CODE,mEdtConfirmCode.getText().toString());
                intent.putExtra(MyConstants.JUMP_TYPE,MyConstants.REGISTER);
                startActivity(intent);
                finish();
            }
        });

        mEdtConfirmCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 0) {
                    isPutConfirmCode = true;
                }else {
                    isPutConfirmCode = false;
                }
                if (count > 0 && isGetConfirmCode && isProtocolChecked){
                    mBtnNext.setClickable(true);
                    mBtnNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnNext.setClickable(false);
                    mBtnNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mCbProtocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    isProtocolChecked = true;
                }else {
                    isProtocolChecked = false;
                }

                if (isPutConfirmCode && isGetConfirmCode && isProtocolChecked){
                    mBtnNext.setClickable(true);
                    mBtnNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnNext.setClickable(false);
                    mBtnNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }
        });

    }

    HttpOnNextListener<BaseResultEntity> mConfirmCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            Toast.makeText(mContext, "验证码已发送", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void getConfirmCode(){
        ApiPhoneIsExist apiPhoneIsExist = new ApiPhoneIsExist(new HttpOnNextListener<Integer>() {

            @Override
            public void onNext(Integer integer) {
                if (integer == 1){
                    CustomToast.show("该手机号已注册");
                }else if (integer == 0){
                    new CountDownTimer(60000,1000){
                        @Override
                        public void onTick(long l) {
                            mBtnGetConfirmCode.setEnabled(false);
                            mBtnGetConfirmCode.setText("获取验证码"+l/1000);
                            mBtnGetConfirmCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
                            isGetConfirmCode = true;
                        }

                        @Override
                        public void onFinish() {
                            mBtnGetConfirmCode.setEnabled(true);
                            mBtnGetConfirmCode.setText("获取验证码");
                            mBtnGetConfirmCode.setTextColor(ContextCompat.getColor(mContext, R.color.color_009944));
                        }
                    }.start();
                    ApiGetConfirmCode apiGetConfirmCode = new ApiGetConfirmCode(mConfirmCodeListener,mContext,true,false,true);
                    Map<String,Object> map = new HashMap<>();
                    map.put("PHONE",mEdtPhone.getText().toString());
                    map.put("MESSAGE_TYPE",1);
                    apiGetConfirmCode.setMap(map);
                    HttpManager.getInstance().doHttpDeal(apiGetConfirmCode);
                }
            }

            @Override
            public void onReLogin() {

            }
        },mContext,true,false,false);

        Map<String,Object> map = new HashMap<>();
        map.put("PHONE",mEdtPhone.getText().toString());
        apiPhoneIsExist.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiPhoneIsExist);
    }


    private void checkConfirmCode() {
        ApiCheckVerificationCode apiCheckVerificationCode = new ApiCheckVerificationCode(new HttpOnNextListener<BeanCity>() {
            @Override
            public void onReLogin() {

            }

            @Override
            public void onNext(BeanCity beanCity) {

            }

            @Override
            public void failed(String massage) {
                super.failed(massage);
                Toast.makeText(mContext, massage, Toast.LENGTH_SHORT).show();
            }
        },mContext,true,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("YZM",mEdtConfirmCode.getText().toString().trim());
        map.put("PHONE", mEdtPhone.getText().toString());
        apiCheckVerificationCode.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiCheckVerificationCode);
    }


}
